<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>贪吃蛇代码 | Hugo Theme Dream</title>

    



<meta name="author" content="Lindixuan" />
<meta name="description" content="" />



<meta name="generator" content="Hugo 0.63.2" />

<link rel="canonical" href="https://lindixuan.gitee.io/%E8%B4%AA%E5%90%83%E8%9B%87%E4%BB%A3%E7%A0%81/" />


<meta property="og:title" content="贪吃蛇代码" />
<meta property="og:description" content="" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://lindixuan.gitee.io/%E8%B4%AA%E5%90%83%E8%9B%87%E4%BB%A3%E7%A0%81/" />
<meta property="article:published_time" content="2020-09-20T12:48:58+08:00" />
<meta property="article:modified_time" content="2020-09-20T12:48:58+08:00" />



<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="贪吃蛇代码"/>
<meta name="twitter:description" content=""/>


<link rel="stylesheet" href="/css/semantic.min.css" />
<link rel="stylesheet" href="/css/OverlayScrollbars.min.css" />
<link rel="stylesheet" href="/css/github-markdown.css" />
<link rel="stylesheet" href="/css/site.css" />


<style>
  a {
    color: seagreen !important;
  }
</style>



<style>
  .inverted a {
     color: darkseagreen !important;
  }
</style>


    


  
    <link rel="stylesheet" data-highlight href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release/build/styles/gruvbox-light.min.css" />
  



  </head>

  
  <body style="background: white;">
  
    
<nav class="ui secondary inverted menu dream-menu">

  <div class="item">
    <i class="large link bullseye icon dream-flip-toggle" title="Flip it!"></i>
  </div>
  <div class="item">
    <i class="large link home icon" title="Home" onclick="window.location.href = 'https:\/\/lindixuan.gitee.io'"></i>
  </div>
  <div class="item">
    <i class="large link icon theme-switch" onclick="themeSwitch()"></i>
  </div>
  
  <div class="item">
    <i class="large link search icon" onclick="toggleSearch()"></i>
  </div>
  
</nav>

    <div class="flip-container">
      <div class="flipper">
        <section class="front">
          <div class="dream-max-width">
            
<div class="ui centered relaxed grid dream-grid">
  <div class="sixteen wide mobile sixteen wide tablet twelve wide computer column markdown-body dream-single">

    <section class="ui top attached segment" id="dream-save-post-as-img">
      <header style="margin-top: 0 !important;">
        <h1 class="ui large header">
          贪吃蛇代码
          <div class="sub header">
            @
            
              Lindixuan
            
            · Sunday, Sep 20, 2020
            · 4 minute read
            · Update at Sep 20, 2020
          </div>
        </h1>
      </header>

      
        <img class="ui image" style="margin-top: 2rem;" src="" />
      

      <article style="margin-top: 2rem;"><pre><code>    #include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;Windows.h&gt;
#include&lt;time.h&gt;
#define MapSizeX 30
#define MapSizeY 20
#define snakeHead 'H'
#define snakeBody 'X'
#define food '$'
#define blank ' '
#define wall '*'
#define up 1
#define down 2
#define right 3
#define left 4

char map[MapSizeY][MapSizeX+1] = {
	&quot;******************************&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;*                            *&quot;,
	&quot;******************************&quot;
};

int direction=right;
int foodnum = 0;

typedef struct snakebody{
	int x, y;
	struct snakebody* next;
}body;

typedef struct snake {
	body* head;
	body* tail;
}snake;

void printMap();
void control(snake*s);
void pause();
void createSnake(snake *s);
void createFood(snake* s);
void forward(body* b);
void move(snake* s);
int biteSelf(snake*s);
int hitWall(snake* s);
void gameOver();
void proceeding();

int main() 
{
	proceeding();
	return 0;
}


void printMap()
{
	system(&quot;cls&quot;);
	
	for (int i = 0; i &lt; MapSizeY; i++)
	{
		/*for (int j = 0; j &lt; MapSizeX; j++)
		{
			printf(&quot;%c&quot;,map[i][j]);
		}*/
		printf(&quot;%s&quot;, map[i]);
		printf(&quot;\n&quot;);
	}
}

void createSnake(snake *s)
{
	s-&gt;head = (body*)malloc(sizeof(body));
	body* p = s-&gt;head;
	p-&gt;x = 5;
	p-&gt;y = 1;
	for (int i = 0; i &lt; 4; i++)
	{
		p-&gt;next = (body*)malloc(sizeof(body));
		p-&gt;next-&gt;y = p-&gt;y;
		p-&gt;next-&gt;x = p-&gt;x - 1;
		p = p-&gt;next;
	}
	p-&gt;next = NULL;
	s-&gt;tail = p;
}

void control(snake*s)
{
	while (1)
	{
		if (GetAsyncKeyState(0x53) &amp;&amp; direction != up)
		{
			direction = down;
		}
		else if (GetAsyncKeyState(0x57) &amp;&amp; direction != down)
		{
			direction = up;
		}
		else if (GetAsyncKeyState(0x41) &amp;&amp; direction != right)
		{
			direction = left;
		}
		else if (GetAsyncKeyState(0x44) &amp;&amp; direction != left)
		{
			direction = right;
		}
		else if (GetAsyncKeyState(VK_SPACE))
		{
			pause();
		}
		Sleep(10);
		move(s);
		if (biteSelf(s) || hitWall(s))
		{
			gameOver();
			break;
		}
		createFood(s);
		printMap();
	}
}

void pause()//暂停
{
	while (1)
	{
		Sleep(1000);
		if (GetAsyncKeyState(VK_SPACE))
		{
			break;
		}

	}
}

int biteSelf(snake* s)
{
	body* b=s-&gt;head-&gt;next;
	while (b != NULL)
	{
		if (b-&gt;x == s-&gt;head-&gt;x &amp;&amp; b-&gt;y == s-&gt;head-&gt;y)
		{
			return 1;
		}
		b = b-&gt;next;
	}
	return 0;
}

int hitWall(snake* s)
{
	body* h = s-&gt;head;
	if (h-&gt;x == MapSizeX - 1 || h-&gt;x == 0 || h-&gt;y == MapSizeY - 1 || h-&gt;y == 0)
		return 1;
	return 0;
}

void createFood(snake* s)
{
	if (foodnum &gt; 0) return;
	srand((unsigned)time(NULL));
	int x, y;
	while (1)
	{
		int valid = 1;
		x = rand() % (MapSizeX-2) + 1;
		y = rand() % (MapSizeY - 2) + 1;
		body* p = s-&gt;head;
		while (p != NULL)
		{
			if (p-&gt;x == x &amp;&amp; p-&gt;y == y)
			{
				valid = 0;
				break;
			}
			p = p-&gt;next;
		}
		if (valid)
		{
			map[y][x] = food;
			foodnum++;
			break;
		}
	}
}

void forward(body* b)
{
	if (b-&gt;next == NULL)
	{	
		return;
	}
	forward(b-&gt;next);
	b-&gt;next-&gt;x = b-&gt;x;
	b-&gt;next-&gt;y = b-&gt;y;
}

void move(snake* s)
{
	body* h = s-&gt;head;
	switch (direction)
	{
	case up:
		if (map[h-&gt;y - 1][h-&gt;x] != food)
		{
			map[s-&gt;tail-&gt;y][s-&gt;tail-&gt;x] = blank;
			forward(s-&gt;head);
			s-&gt;head-&gt;y--;
		}
		else
		{
			s-&gt;tail-&gt;next = (body*)malloc(sizeof(body));
			s-&gt;tail = s-&gt;tail-&gt;next;
			s-&gt;tail-&gt;next = NULL;
			forward(s-&gt;head);
			s-&gt;head-&gt;y--;
			foodnum--;
		}
		break;
	case down:
		if (map[h-&gt;y + 1][h-&gt;x] != food)
		{
			map[s-&gt;tail-&gt;y][s-&gt;tail-&gt;x] = blank;
			forward(s-&gt;head);
			s-&gt;head-&gt;y++;
		}
		else
		{
			s-&gt;tail-&gt;next = (body*)malloc(sizeof(body));
			s-&gt;tail = s-&gt;tail-&gt;next;
			s-&gt;tail-&gt;next = NULL;
			forward(s-&gt;head);
			s-&gt;head-&gt;y++;
			foodnum--;
		}
		break;
	case right:
		if (map[h-&gt;y][h-&gt;x+1] != food)
		{
			map[s-&gt;tail-&gt;y][s-&gt;tail-&gt;x] = blank;
			forward(s-&gt;head);
			s-&gt;head-&gt;x++;
		}
		else
		{
			s-&gt;tail-&gt;next = (body*)malloc(sizeof(body));
			s-&gt;tail = s-&gt;tail-&gt;next;
			s-&gt;tail-&gt;next = NULL;
			forward(s-&gt;head);
			s-&gt;head-&gt;x++;
			foodnum--;
		}
		break;
	case left:
		if (map[h-&gt;y][h-&gt;x - 1] != food)
		{
			map[s-&gt;tail-&gt;y][s-&gt;tail-&gt;x] = blank;
			forward(s-&gt;head);
			s-&gt;head-&gt;x--;
		}
		else
		{
			s-&gt;tail-&gt;next = (body*)malloc(sizeof(body));
			s-&gt;tail = s-&gt;tail-&gt;next;
			s-&gt;tail-&gt;next = NULL;
			forward(s-&gt;head);
			s-&gt;head-&gt;x--;
			foodnum--;
		}
		break;
	}
	body* p = s-&gt;head-&gt;next;
	map[s-&gt;head-&gt;y][s-&gt;head-&gt;x] = snakeHead;
	while (p != NULL)
	{
		map[p-&gt;y][p-&gt;x] = snakeBody;
		p = p-&gt;next;
	}
}

void gameOver()
{
	system(&quot;cls&quot;);
	printf(&quot;game over!\n&quot;);
}

void proceeding()
{
	snake s;
	createSnake(&amp;s);
	for (int i = 0; i &lt; MapSizeX; i++) map[0][i] = wall;
	for (int i = 1; i &lt; MapSizeY - 1; i++)
	{
		map[i][0] = wall;
		map[i][MapSizeX - 1] = wall;
		for (int j = 1; j &lt; MapSizeX - 1; j++) map[i][j] = blank;
	}
	for (int i = 0; i &lt; MapSizeX; i++) map[MapSizeY-1][i] = wall;
	
	control(&amp;s);
		
}
</code></pre></article>
    </section>

    <footer class="ui attached segment dream-tags">
      
          <a class="ui label">No Tags</a>
      
      <div
        class="ui label"
        style="float: right; cursor: pointer;"
        onclick="savePostAsImg()">
        <i class="save icon"></i>Save as image
      </div>
    </footer>

    

    
    
    

  </div>
  <div class="sixteen wide mobile sixteen wide tablet four wide computer column">
    <article class="dream-header">
  <section class="ui top attached center aligned segment">
    <div class="ui small circular image">
      
        <img src="/img/sleep.jpg">
      
    </div>

    
    <h1 class="ui medium header">Lindixuan&#39;s blog<div class="sub header" style="margin-top: 0.5rem;">好耶！</div>
    </h1>
    

    <div class="ui horizontal list">
      
      <a class="item" href="/tags">
        <i class="tags icon" title="All Tags"></i>
      </a>
      <a class="item" href="/categories">
        <i class="th list icon" title="All Categories"></i>
      </a>
    </div>
  </section>

  

  

  <section class="ui attached segment header-socials">
    <nav class="ui secondary menu dream-menu dream-socials">
  
  
    <div class="item">
      <a href="mailto:1228210482@qq.com">
        <i class=" mail icon" title="Email"></i>
      </a>
    </div>
  

  

  

  

  

  

  

  
</nav>

  </section>

  <section class="ui bottom attached center aligned segment">
    
      <p>© 2020 - 2021 Hugo Theme Dream</p>
    

    <p>Powered by <a href="https://gohugo.io/" target="_blank">Hugo</a> with theme <a href="https://github.com/g1eny0ung/hugo-theme-dream" target="_blank">Dream</a>.</p>

    
  </section>
</article>

  </div>
</div>

          </div>
        </section>
        <section class="back">
          <div class="dream-max-width">
            <div class="ui centered relaxed grid dream-grid dream-back">
  
  
  

  <section class="sixteen wide mobile eight wide tablet four wide computer column dream-column">
    <article>
      <div class="ui top attached segment">
        <h3 class="ui header">Social Links</h3>
      </div>
      <div class="ui bottom attached segment">
        <nav class="ui secondary menu dream-menu dream-socials">
  
  
    <div class="item">
      <a href="mailto:1228210482@qq.com">
        <i class="large mail icon" title="Email"></i>
      </a>
    </div>
  

  

  

  

  

  

  

  
</nav>

      </div>
    </article>
  </section>

  <section class="sixteen wide mobile eight wide tablet four wide computer column dream-column">
    
  </section>

  
    
  
</div>

          </div>
        </section>
      </div>
    </div>

    <script>
  window.darkNav =  true 
</script>
<script src="/js/jquery.min.js"></script>
<script src="/js/semantic.min.js"></script>
<script src="/js/jquery.overlayScrollbars.min.js"></script>
<script src="/js/header.js"></script>
<script src="/js/main.js"></script>
<script src="/js/theme.js"></script>

    
<script src="/js/html2canvas.min.js"></script>
<script src="/js/post.js"></script>


  <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release/build/highlight.min.js"></script>

  
    
      <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release/build/languages/ocaml.min.js"></script>
     
  

  <script>
  hljs.initHighlightingOnLoad()
  setHighlightTheme()

  function setHighlightTheme() {
    var isDark = localStore.getItem('hugo-theme-dream-is-dark')
    var lightTheme = "gruvbox-light"
    var darkTheme = "gruvbox-dark"
    var theme = isDark ? darkTheme : lightTheme

    $('link[data-highlight]').attr('href', 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release/build/styles/' + theme + '.min.css')
    $('pre').css('background', isDark ? '#333' : '')
  }
</script>





    
      <div class="ui inverted segment" id="dream-search">
  <div class="ui search">
    <div class="ui transparent input">
      <input class="prompt" type="text" placeholder="Search" />
    </div>
    <div class="results"></div>
  </div>
</div>
<script>
  $(document).ready(function () {
    $.getJSON('https:\/\/lindixuan.gitee.io/index.json', function (data) {
      $('.ui.search').search({
        source: data,
        searchFields: ['title'],
        showNoResults: true,
      })
    })
  })
</script>
<script src="/js/search.js"></script>

    

    
  </body>
</html>
